package com.Drools.敲定的几种模式;

import com.Drools.CIPS.bean.DroolsReturn;
import com.Drools.CIPS.ruler.Xlsx_MTRuler;
import com.Drools.CIPS.util.MTToSwiftMessage;
import com.prowidesoftware.swift.io.parser.SwiftParser;
import com.prowidesoftware.swift.model.SwiftBlock4;
import com.prowidesoftware.swift.model.SwiftMessage;
import com.prowidesoftware.swift.model.field.Field;
import com.prowidesoftware.swift.model.mt.mt2xx.MT296;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MT {
    public static void main(String[] args) {
        SwiftMessage swift =
                MT.swift("D:\\Work\\IDEA\\Drools\\src\\main\\resources\\MT\\MT296-6.txt");

        SwiftBlock4 block4 = swift.getBlock4();
        MT296 mt296 = new MT296(swift);
        for (Field f:mt296.getFields()){
            System.out.println(f.getName());

        }
        System.out.println();

    }

    public static SwiftMessage swift(String path){
        String mt103 = fileToString(path);
//        System.out.println(mt103);
        // 方法一
        SwiftParser parser = new SwiftParser(mt103);
        SwiftMessage swiftMessage = null;
        try {
            swiftMessage = parser.message();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return swiftMessage;
    }

    private static String fileToString(String path) {
        StringBuilder sb = new StringBuilder("");
        try(BufferedReader br = new BufferedReader(new FileReader(path));) {
            char[] cbuf = new char[1024];
            int len;
            while ((len = br.read(cbuf)) != -1){
                sb.append(cbuf,0,len);
                //由于汉字等某些字符  一个字符占不等的 字节 所以有汉字的用字符流好
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}
